package com.baiduspring.function.ucpass;

import com.baiduspring.function.ucpass.client.AbsRestClient;
import com.baiduspring.function.ucpass.client.JsonReqClient;
import com.baiduspring.function.ucpass.client.XmlReqClient;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * 短信验证码发送接口
 *
 * @author 熊彦
 */
public class SmsUtil {
    private static volatile SmsUtil util = null;

    public static SmsUtil getInstance() {
        if (util == null) {
            synchronized (SmsUtil.class) {
                if (util == null) {
                    util = new SmsUtil();
                }
            }
        }
        return util;
    }

    static AbsRestClient instantiationRestAPI(boolean enable) {
        if (enable) {
            return new JsonReqClient();
        } else {
            return new XmlReqClient();
        }
    }

    /**
     * 相关的参数见云之讯接口说明文档
     *
     * @param templateId
     * @param to
     * @param param
     * @return
     */
    public String send(String templateId, String to, String param) {
		Properties properties = new Properties();
		InputStream in = getClass().getResourceAsStream("/ucpaas.config.properties");
		try {
			properties.load(in);
		} catch (IOException e) {
			e.printStackTrace();
		}

		String accountSid = properties.getProperty("accountSid");
		String token = properties.getProperty("token");
		String appId = properties.getProperty("appId");

        try {
            String result = instantiationRestAPI(true).templateSMS(accountSid, token, appId, templateId, to, param);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
